package cc.lishixi.book.service;

import cc.lishixi.book.dao.mapper.BookInfoRowMapper;
import cc.lishixi.book.entity.BookInfo;
import cc.lishixi.book.service.api.AbstractService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by LiShixi on 2016/7/24 0024.
 * http://www.lishixi.cc
 */
@Service
public class BookInfoService extends AbstractService<BookInfo> {

    @Override
    public List<BookInfo> list() {
        return jdbcTemplate.query("select * from bookinfo", new BookInfoRowMapper());
    }

    @Override
    public boolean delete(Integer id) {
        return jdbcTemplate.update("delete from bookinfo where id=?", id) > 0;
    }

    @Override
    public boolean edit(BookInfo t) {
        String sql = "UPDATE `book`.`bookinfo` SET `barcode`=?, `isbn`=?, `status`=?, `duedate`=?, `location`=? WHERE  `id`=? LIMIT 1;";
        return jdbcTemplate.update(sql, t.getBarcode(), t.getIsbn(), t.getStatus(), t.getDuedate(), t.getLocation(), t.getId()) > 0;
    }

    @Override
    public boolean add(BookInfo t) {
        String sql = "INSERT INTO `book`.`bookinfo` (`barcode`, `isbn`, `status`, `duedate`, `location`) VALUES (?, ?, ?, ?, ?);";
        return jdbcTemplate.update(sql, t.getBarcode(), t.getIsbn(), t.getStatus(), t.getDuedate(), t.getLocation()) > 0;
    }

    /**
     * 检查书是否可借
     * @param isbn
     * @return
     */
    public boolean isBorrow(String isbn){
        String sql="select * from bookinfo where isbn=? and status=1";
        return jdbcTemplate.query(sql,new BookInfoRowMapper(), new Object[]{isbn}).size()>0;

    }
}
